{% extends "park/base.html" %}

{% block css %}
{% endblock %}

{% block content %}
<script type="text/javascript">
    var primary_menu = "menu_3";
    var sub_menu = "menu_3_0";
</script>
            <div class="col-md-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <div class="panel-title"><h3>生产区</h3></div>
                        <div class="panel-options">
                            <form role="form" action="" method="post" enctype="" class="form-horizontal pull-left">
                                <div class="form-group">
                                    <label class="control-label">公司：</label>
                                    <div>
                                        <input id='f_company' class="time company" type="text" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">时间：</label>
                                    <div>
                                        <input id='f_date' class="Wdate time" type="text" name="annual_produce_time" value="" onfocus="WdatePicker({dateFmt:'yyyy-MM'})" datatype="n">
                                    </div>
                                </div>
                                <div class="form-group filter-btn">
                                  <a href='javascript:filterDate();' class="btn btn-green">筛选</a>
                                </div>
                            </form>
                            <a href="#" class="bg">
                            <!-- <button type="button" class="btn btn-success">添加</button></a> -->
                            <!-- <a href="#" data-rel="collapse"><i class="entypo-down-open"></i></a> -->
                            <!--<a href="#" data-rel="reload"><i class="entypo-arrows-ccw"></i></a>-->
                        </div>
                        <!-- <div class="stay_right">
                            <a href="/chemicals/productAdd" class="btn btn-success">添加</a>
                        </div> -->
                    </div>
                    <div class="panel-body with-table">
                        <table class="table table-bordered table-responsive">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>主要设备名称</th>
                                <th>数量</th>
                                <th>所在生产单元</th>
                                <th>生产的产品编号</th>
                                <th>风险特征</th>
                                <th>所属公司</th>
                                <th>添加时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                                {% for p in ps %}
                                    <tr>
                                        <td>{{ forloop.counter }}</td>
                                        <td>{{ p.name }}</td>
                                        <td>{{ p.count }}</td>
                                        <td>{{ p.in_unit }}</td>
                                        <td>{{ p.code }}</td>
                                        <td>{{ p.risk }}</td>
                                        <td>{{ p.enterprise.name }}</td>
                                        <td>{{ p.add_time|date:"Y-m-d" }}</td>
                                        <td>
                                            <a href="/park/facilities/productionDetail/{{ p.id }}" class="btn btn-success">
                                                查看
                                            </a>
                                        </td>
                                    </tr>
                                {% empty %}
                                    <tr>
                                        <td colspan=9 style='text-align:center;'>暂无数据</td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                        <div class="row">
                            <div class="col-xs-6 col-left">
                                <div class="dataTables_info" id="table-3_info"></div>
                            </div>
                            <div class="col-xs-6 col-right">
                                <div class="dataTables_paginate paging_bootstrap" style="text-align: right;margin-right: 10px">
                                    <ul class="pagination pagination-sm" >
                                        {% if ps.has_previous %}
                                            <li><a href="?page={{ ps.previous_page_number }}">上一页</a></li>
                                        {% else %}
                                            <li class="disabled"> <span>上一页</span></li>
                                        {% endif %}
                                        {% for p in paginator.page_range %}
                                            {% ifequal p ps.number %}
                                                <li class="active"> <span class="p">{{p}}</span></li>
                                            {% else%}
                                                <li> <a href="?page={{ p }}">{{p}}</a></li>
                                            {% endifequal %}
                                        {% endfor %}
                                        {% if ps.has_next %}
                                            <li class="next">  <a href="?page={{ ps.next_page_number }}">下一页</a></li>
                                        {% else %}
                                            <li class="disabled"><span>下一页</span></li>
                                        {% endif %}
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

{% endblock %}

{% block js %}

<script src="/static/js/date-choose.js"></script>
<script language="javascript">

    var ep_data = [
        {% for e in es %}
            { "id":{{ e.id }}, "value": "{{ e.name }}", "label": "{{ e.name }}" },
        {% endfor %}
    ];

    $(function () {
        $('#f_company').bind('click',function(){
            $('#f_company')[0].setAttribute('eid', '');
            console.log(123);
        })

        $('#f_company').autocompleter({ 
            source: ep_data,
            limit: 5,
            callback: function (value, index) {
                $('#f_company')[0].setAttribute('eid', ep_data[index].id);
            }
        });
    });

    function getNextElement (field) {
        var form = field.form;
        for (var e = 0; e < form.elements.length; e++)
            if (field == form.elements[e])
                break;

        return form.elements[++e % form.elements.length];
    }
    function tabOnEnter (field, evt) {
        var keyCode = document.layers ? evt.which : document.all ?
                evt.keyCode : evt.keyCode;
        if (field.value.length == 1){
            getNextElement(field).focus();
            return false;
        }
    }

    window.onload = function(){
        var y = $.urlGet()['y'];
        var m = $.urlGet()['m']

        if(!y || !m){
            return false;
        }

        $('#f_date').val(y+'-'+m);
    }

    function filterDate(){
        var f_date = $('#f_date').val();

        var f_company = $('#f_company')[0];
        var f_company_name = f_company.value;
        var f_company_id = f_company.getAttribute('eid');

        if(!f_date && !f_company_name){
            window.location.href = '/park/chemicals/productList/';
            return false;
        }

        f_date = f_date.split('-');

        var param = {
            'y':f_date[0],
            'm':f_date[1]
        }

        if(f_company_id){
            param['eid'] = f_company_id;
        }else{
            param['ename'] = f_company_name;
        }

        param = $.param(param, true);
        
        window.location.href =  window.location.pathname + '?' + param;
    }
</script>
{% endblock %}